﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.OleDb;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            IspolniIme();
        }
    }
    


    protected void IspolniIme() 
    {
        ListBox1.Items.Clear();

        string conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;

        OleDbConnection conn = new OleDbConnection(conString);

        OleDbCommand komanda = new OleDbCommand();

        komanda.Connection = conn;

        komanda.CommandText = "select ID,grad from Table1";

        try
        {
            conn.Open();

            OleDbDataReader citaj = komanda.ExecuteReader();

            while (citaj.Read())
            {
                ListItem element = new ListItem();
                element.Text = citaj["grad"].ToString();
                element.Value = citaj["ID"].ToString();
                ListBox1.Items.Add(element);
            }

            citaj.Close();
        }
        catch(Exception ex)
        {
            lblStatus.Text = ex.Message;
        }
        finally
        {
            conn.Close();
        }

    }

  
 
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SelectAvtor(ListBox1.SelectedValue);
    }  
     


    protected void SelectAvtor(string ID) 
    {

        string conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;

        OleDbConnection conn = new OleDbConnection(conString);


        OleDbCommand komanda = new OleDbCommand();

        komanda.Connection = conn;
        // int pole = (Int32)Session["pole"];
        komanda.CommandText = "SELECT * FROM Table1 WHERE ID =" + ID ; // + pole;
      //  komanda.CommandText = "select ID,ime,grad,opis,taksa,cena from Table1 where ID = 3";


        try
        {
            conn.Open();

            OleDbDataReader citaj = komanda.ExecuteReader();

            if (citaj.Read())
            {
                
                Label6.Text = citaj["ID"].ToString();
                Label1.Text = citaj["ime"].ToString();
                Label2.Text = citaj["grad"].ToString();
                Label3.Text = citaj["specijalitet"].ToString();
                Label5.Text = citaj["cena"].ToString();

                citaj.Close();
            }
        }

        catch(Exception error)
        {
            lblStatus.Text = error.Message;
        }

        finally
        {
            conn.Close();
        }
    
}

    protected void ListBox1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        SelectAvtor(ListBox1.SelectedValue);
    }


    
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (ListBox1.SelectedIndex != -1)
        {
            zemiSaldo();

            string saldo = (string)ViewState["saldo"];
            int sal = Convert.ToInt32(saldo);
            int cena = Convert.ToInt32(Label5.Text);

            if (sal >= cena)
            {
                odzemiOdSaldo(sal - cena);
            }
            else
            {
                lblStatus.Text = "Немате доволно пари БАНКРОТ :(";
            }
           
        }
    }

    void zemiSaldo()
    {
        string conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;

        OleDbConnection conn = new OleDbConnection(conString);


        OleDbCommand komanda = new OleDbCommand();

        komanda.Connection = conn;
       // int idKorisnik = (Int32)Session["ID"];
        komanda.CommandText = "SELECT * FROM Table2 WHERE ID =2"; //+idKorisnik;
        //  komanda.CommandText = "select ID,ime,grad,opis,taksa,cena from Table1 where ID = 3";


        try
        {
            conn.Open();

            OleDbDataReader citaj = komanda.ExecuteReader();

            if (citaj.Read())
            {
                string saldo = citaj["saldo"].ToString();
                ViewState["saldo"] = saldo;
                
                citaj.Close();
            }
        }

        catch (Exception error)
        {
            lblStatus.Text = error.Message;
        }

        finally
        {
            conn.Close();
        }
    
    }


    void odzemiOdSaldo(int novoSaldo)
    {
        string conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;

        OleDbConnection conn = new OleDbConnection(conString);


        OleDbCommand komanda = new OleDbCommand();

        komanda.Connection = conn;

        komanda.CommandText = "UPDATE Table2 SET saldo= "+novoSaldo+ " WHERE ID=2";
        //  komanda.CommandText = "select ID,ime,grad,opis,taksa,cena from Table1 where ID = 3";


        try
        {
            conn.Open();

            komanda.ExecuteNonQuery();
            lblStatus.Text = "Успешна трансакција :)";

        }

        catch (Exception error)
        {
            lblStatus.Text = error.Message;
        }

        finally
        {
            conn.Close();
        }
    }
}
